package priv.lan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import priv.lan.pojo.User;
import priv.lan.pojo.vo.UserQuery;
import priv.lan.service.UserService;
import priv.lan.util.R;

import java.util.List;

/**
 * @Description: TODO
 * @author: lan
 * @date: 2021/12/3 13:16
 * @Version: 1.0
 */
@Api("UserController")
@CrossOrigin
@RestController
@RequestMapping("/admin/user")
public class AdminController {
    @Autowired
    UserService userService;

    //login
    @PostMapping("login")
    public R login() {
        return R.ok().data("token","admin");
    }
    //info
    @GetMapping("info")
    public R info() {
        return R.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }

    //条件查询带分页的方法  当前页，记录数
    @PostMapping("pageTourist/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false) UserQuery userQuery) {
        //创建page对象
        Page<User> pageUser = new Page<>(current,limit);
        //构建条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = userQuery.getUsername();
        String phone = userQuery.getPhone();
        String begin = userQuery.getBegin();
        String end = userQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!ObjectUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("username",name);
        }
        if(!ObjectUtils.isEmpty(phone)) {
            wrapper.eq("phone",phone);
        }
        if(!ObjectUtils.isEmpty(begin)) {
            wrapper.ge("create_time",begin);
        }
        if(!ObjectUtils.isEmpty(end)) {
            wrapper.le("create_time",end);
        }

        //排序
        wrapper.orderByDesc("create_time");

        //未删除的记录
        wrapper.eq("is_deleted", 0);
        //调用方法实现条件查询分页
        userService.page(pageUser,wrapper);

        long total = pageUser.getTotal();//总记录数

        List<User> records = pageUser.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }


    @PostMapping("deletebyid/{id}")
    public R deletebyID(@PathVariable String id){
//        逻辑删除 将is_deleted 变成 1
        if (userService.deletByLogic(id)){
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("getUserById/{id}")
    public R getUserById(@PathVariable String id){
        User user = userService.getById(id);
        if (user!=null){
            return R.ok().data("user",user);
        }else {
            return R.error();
        }
    }
    @PostMapping("updateUserById")
    public R updateUserById(@RequestBody User user){

        boolean isSave = userService.updateById(user);
        if (isSave){
            return R.ok();
        }else {
            return R.error();
        }
    }

}
